﻿using Newtonsoft.Json;
using System;
using XiAnAirport.BUS.RocketMQ;
using XiAnAirport.Models;

namespace XiAnAirport.BUS
{
    public class LiftDataSyncBUS
    {
        public ResponseMessage ThirdPartyDataProcess(ThirdPartyData thirdPartyData)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                string version = FXMQClient.version;
                if (thirdPartyData.dataType == version + "/DeviceStatusTotal/01" || thirdPartyData.dataType == version + "/DeviceStatusTotal/02" || thirdPartyData.dataType == version + "/DeviceStatusTotal/03")
                {
                    //LogHelper.Log(thirdPartyData.data.ToString());
                    DeviceStatusTotal deviceStatusTotal = JsonConvert.DeserializeObject<DeviceStatusTotal>(thirdPartyData.data.ToString());
                    if (deviceStatusTotal != null)
                    {
                        MqttDataBUS mqttDataBUS = new MqttDataBUS();
                        mqttDataBUS.MqttDataProcess(deviceStatusTotal, "DeviceStatusTotal", thirdPartyData.dataType.Substring(thirdPartyData.dataType.Length - 2, 2));
                    }
                }
                else if (thirdPartyData.dataType == version + "/TodayProcess/01" || thirdPartyData.dataType == version + "/TodayProcess/02" || thirdPartyData.dataType == version + "/TodayProcess/03")
                {
                    MqttDataBUS mqttDataBUS = new MqttDataBUS();
                    TodayProcess todayProcess = JsonConvert.DeserializeObject<TodayProcess>(thirdPartyData.data.ToString());
                    if (todayProcess != null)
                    {
                        mqttDataBUS.MqttDataProcess(todayProcess, "TodayProcess", thirdPartyData.dataType.Substring(thirdPartyData.dataType.Length - 2, 2));
                    }
                }
                else if (thirdPartyData.dataType == version + "/TodayRuntime/01" || thirdPartyData.dataType == version + "/TodayRuntime/02" || thirdPartyData.dataType == version + "/TodayRuntime/03")
                {
                    MqttDataBUS mqttDataBUS = new MqttDataBUS();
                    TodayRuntime todayRuntime = JsonConvert.DeserializeObject<TodayRuntime>(thirdPartyData.data.ToString());
                    if (todayRuntime != null)
                    {
                        string id = thirdPartyData.dataType.Substring(thirdPartyData.dataType.Length - 2, 2);
                        string manufacturer = string.Empty;
                        if (id == "01")
                        {
                            manufacturer = "Schindler";
                        }
                        else if (id == "02")
                        {
                            manufacturer = "TKE";
                        }
                        else
                        {
                            manufacturer = "TOSHIBA";
                        }
                        mqttDataBUS.MqttDataProcess(id, manufacturer, "TodayRuntime", todayRuntime.runtime);
                    }
                }
                else if (thirdPartyData.dataType == version + "/FaultStatistics/01" || thirdPartyData.dataType == version + "/FaultStatistics/02" || thirdPartyData.dataType == version + "/FaultStatistics/03")
                {
                    MqttDataBUS mqttDataBUS = new MqttDataBUS();
                    FaultStatistics faultStatistics = JsonConvert.DeserializeObject<FaultStatistics>(thirdPartyData.data.ToString());
                    if (faultStatistics != null)
                    {
                        mqttDataBUS.MqttFaultStatistics(thirdPartyData.dataType.Substring(thirdPartyData.dataType.Length - 2, 2), "FaultStatistics", faultStatistics.faultTotal, faultStatistics.faultToday);
                    }
                }
                else if (thirdPartyData.dataType == version + "/FaultRecordReport/01" || thirdPartyData.dataType == version + "/FaultRecordReport/02" || thirdPartyData.dataType == version + "/FaultRecordReport/03")
                {
                    MqttDataBUS mqttDataBUS = new MqttDataBUS();
                    FaultRecordReport faultRecordReport = JsonConvert.DeserializeObject<FaultRecordReport>(thirdPartyData.data.ToString());
                    if (faultRecordReport != null)
                    {
                        string id = thirdPartyData.dataType.Substring(thirdPartyData.dataType.Length - 2, 2);
                        string manufacturer = string.Empty;
                        if (id == "01")
                        {
                            faultRecordReport.manufacturer = "Schindler";
                        }
                        else if (id == "02")
                        {
                            faultRecordReport.manufacturer = "TKE";
                        }
                        else
                        {
                            faultRecordReport.manufacturer = "TOSHIBA";
                        }

                        mqttDataBUS.MqttFaultRecordDataProcess(faultRecordReport);
                    }
                }
            }
            catch (Exception ex)
            {
                message.errcode = 1;
            }
            return message;
        }
    }
}
